package com.qingke.hibernate.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.query.Query;

import com.qingke.hibernate.bean.Student;
import com.qingke.hibernate.bean.StudentStatus;

public class StudentDAO extends SystemDAO{
	
	public StudentDAO(Session session) {
		super(session);
	}
	public List<Student> list(){
		Query<Student> query=session.createQuery("from Student", Student.class);
		return query.getResultList();
	}
	public Student getStudentById(int id){
		Query<Student> query=session.createQuery("from Student where id=:id", Student.class);
		query.setParameter("id", id);
//		return query.getResultList().get(0);
		return query.getSingleResult();
	}
	
	public Student getStudentBySid(int sid){
		Query<Student> query=session.createQuery("from Student where sid=:sid", Student.class);
		query.setParameter("sid", sid);
		return query.getSingleResult();
	}
	
	public List<Student> listStudentsByName(String name){
		Query<Student> query=session.createQuery("from Student where firstname like :name or lastname like :name", Student.class);
		query.setParameter("name", "%"+name+"%");
		return query.getResultList();
	}
	
	public Student listStudentByPhone(String phone){
		Query<Student> query=session.createQuery("from Student where phone=:phone", Student.class);
		query.setParameter("phone", phone);
		return query.getSingleResult();
	}
	
	public Student listStudentByEmail(String email){
		Query<Student> query=session.createQuery("from Student where email=:email", Student.class);
		query.setParameter("email", email);
		return query.getSingleResult();
	}
	
	public List<Student> listStudentsByGender(String gender){
		Query<Student> query=session.createQuery("from Student where gender=:gender", Student.class);
		query.setParameter("gender", gender);
		return query.getResultList();
	}
	
	public List<Student> listStudentsByAge(int age){
		Query<Student> query=session.createQuery("from Student where age=:age", Student.class);
		query.setParameter("age", age);
		return query.getResultList();
	}
	
	public List<Student> listStudentsByStatus(StudentStatus status){
		Query<Student> query=session.createQuery("from Student where student_status_id=:status", Student.class);
		query.setParameter("status", status.getId());
		return query.getResultList();
	}
	
}
